package com.yohann.boot.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbit mq 配置类
 *
 * @author Yeung Wah
 * @since 2021/9/12
 */
@Configuration
@ConditionalOnBean(ConnectionFactory.class)
public class RabbitMqConfig {
    @Value("${rabbit.mq.exchange}")
    private String exchange;
    @Value("${rabbit.mq.dead.letter.exchange}")
    private String deadLetterExchange;
    @Value("${rabbit.mq.queue}")
    private String queue;
    @Value("${rabbit.mq.dead.letter.queue}")
    private String deadLetterQueue;
    @Value("${rabbit.mq.routing.key}")
    private String routingKey;
    @Value("${rabbit.mq.dead.letter.routing.key}")
    private String deadLetterRoutingKey;
    @Value("${rabbit.mq.ttl}")
    private long ttl;

    @Bean("directExchange")
    public DirectExchange getDirectExchange() {
        return new DirectExchange(exchange);
    }

    @Bean("deadLetterExchange")
    public DirectExchange getDeadLetterExchange() {
        return new DirectExchange(deadLetterExchange);
    }

    @Bean("queue")
    public Queue getQueue() {
        Map<String, Object> arguments = new HashMap<>(4);
        arguments.put("x-dead-letter-exchange", deadLetterExchange);
        arguments.put("x-dead-letter-routing-key", deadLetterRoutingKey);
        arguments.put("x-message-ttl", ttl);

        return QueueBuilder.durable(queue)
                .withArguments(arguments)
                .build();
    }

    @Bean("queueTtl")
    public Queue getQueueTtl() {
        Map<String, Object> arguments = new HashMap<>(4);
        arguments.put("x-dead-letter-exchange", deadLetterExchange);
        arguments.put("x-dead-letter-routing-key", deadLetterRoutingKey);

        return QueueBuilder.durable(queue + "Ttl")
                .withArguments(arguments)
                .build();
    }

    @Bean("deadLetterQueue")
    public Queue getDeadLetterQueue() {
        return QueueBuilder.durable(deadLetterQueue).build();
    }

    @Bean
    public Binding getBindingProvider(@Qualifier("queue") Queue queue, @Qualifier("directExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingKey);
    }

    @Bean
    public Binding getBindingProviderTtl(@Qualifier("queueTtl") Queue queue, @Qualifier("directExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingKey + "Ttl");
    }

    @Bean
    public Binding getBindingConsumer(@Qualifier("deadLetterQueue") Queue queue, @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(deadLetterRoutingKey);
    }
}